package com.smart.home.controller.Devices;

import com.smart.home.dto.Devices.DevicesTypeDto;
import com.smart.home.entity.DeviceTypes;
import com.smart.home.entity.Devices;
import com.smart.home.result.Result;
import com.smart.home.service.DevicesTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/devicesType")
@Slf4j
@Api(tags = "设备类型")
public class DevicesTypeController {


    @Autowired
    private DevicesTypeService devicesTypeService;


    /**
     * 添加设备类型
     * @param devicesTypeDto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加设备类型")
    public Result<Long> addDevice(@RequestBody DevicesTypeDto devicesTypeDto) {
        log.info("添加设备类型");
        Long id = devicesTypeService.addDevice(devicesTypeDto);
        return Result.success(id);
    }

    /**
     * 修改设备类型
     * @param devicesTypeDto
     * @return
     */
    @PostMapping("/update/{id}")
    @ApiOperation("修改设备类型")
    public Result<Long> updateDevice(@RequestBody DevicesTypeDto devicesTypeDto,@PathVariable("id") Long deviceTypeId) {
        log.info("修改设备类型");
        Long id = devicesTypeService.updateDevice(devicesTypeDto,deviceTypeId);
        return Result.success(id);
    }




    /**
     * 查询相关家庭的相关设备
     * @param id
     * @return
     */
    @PostMapping("/query/{id}")
    @ApiOperation("查询家庭所有设备类型")
    public Result<List<DeviceTypes >> queryDeviceTypes(@PathVariable Long id){
        log.info("通过id查询设备类型");
        return Result.success(devicesTypeService.queryDeviceTypes(id));
    }

    /**
     * 查询设备类型下的设备
     * @param id
     * @return
     */
    @PostMapping("/queryDevice/{id}")
    @ApiOperation("查询设备类型下的设备")
    public Result<List<Devices>> queryDevice(@PathVariable Long id){
        log.info("查询设备类型下的设备");
        return Result.success(devicesTypeService.queryDevice(id));
    }

    /**
     * 移除设备
     * @param DeviceId
     * @return
     */
    @PostMapping("/removeDevice/{id}")
    @ApiOperation("移除设备")
    public Result removeDevice(@PathVariable("id") Long DeviceId){
        log.info("移除设备");
        devicesTypeService.removeDevice(DeviceId);
        return Result.success();
    }

    //删除设备类型
    @PostMapping("/removeDeviceType/{id}")
    @ApiOperation("删除设备类型")
    public Result removeDeviceType(@PathVariable Long id){
        log.info("删除设备类型");
        if(id == 0){
            return Result.error("设备类型id不能为0");
        }
        devicesTypeService.removeDeviceType(id);
        return Result.success();
    }

    //将设备从待机状态类型改成其他类型
    @PostMapping("/changeDeviceType")
    @ApiOperation("将设备从待机状态类型改成其他类型")
    public Result changeDeviceType(Long DeviceId , Long DeviceTypeId){
        log.info("将设备从待机状态类型改成其他类型");
        devicesTypeService.changeDeviceType(DeviceId,DeviceTypeId);
        return Result.success();
    }



}
